package com.ecnu.web.controller.tool;

import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.system.domain.SoftwareTool;
import com.ecnu.system.domain.dto.EvaluateListDTO;
import com.ecnu.system.domain.dto.SoftwareDTO;
import com.ecnu.system.domain.dto.SoftwareUserDTO;
import com.ecnu.system.domain.vo.SoftwareToolVO;
import com.ecnu.system.service.ISoftwareToolService;
import com.ecnu.system.service.ISoftwareUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "软件工具")
@RestController
@RequestMapping("/software")
public class SoftwareToolController extends BaseController {
    @Autowired
    private  ISoftwareToolService softwareToolService;
    @Autowired
    private ISoftwareUserService softwareUserService;

    private static LoginUser getProUser() {
        LoginUser loginUser = new LoginUser();
        //看情况是否需要优化--获取登录信息问题
        try {
            loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {

        }
        return loginUser;
    }

    @ApiOperation(value = "工具列表-前台")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody @Valid EvaluateListDTO dto) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        LoginUser loginUser = getProUser();
        return softwareToolService.getListForUser(dto, loginUser);
    }
    @ApiOperation(value = "工具授权")
    @PostMapping("/grant")
    public AjaxResult grant(@RequestBody @Valid SoftwareUserDTO dto) {
        LoginUser loginUser = getLoginUser();
        return softwareUserService.grant(dto, loginUser);
//        return null;
    }

    @ApiOperation(value = "工具列表-后台")
    @PostMapping("/adminList")
    @PreAuthorize("@ss.hasPermi('software:adminList')")
    public TableDataInfo getSoftware(@RequestBody@Valid EvaluateListDTO dto) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        LoginUser loginUser = getLoginUser();
        return softwareToolService.getList(dto, loginUser);
    }

    @ApiOperation(value = "工具明细")
    @GetMapping("/info")
    @PreAuthorize("@ss.hasPermi('software:info')")
    public AjaxResult info(String id){
        LoginUser loginUser = getLoginUser();
        SoftwareTool byId = softwareToolService.getById(Integer.valueOf( id));
        SoftwareToolVO vo = new SoftwareToolVO();
        BeanUtils.copyProperties(byId, vo);
        vo.setId(String.valueOf(byId.getId()));
        
        // 转换图片二进制数据为Base64
//        if (byId.getImageData() != null) {
//            String base64Image = byId.getImageData();
//            vo.setImageBase64(base64Image);
//        }
        
        return AjaxResult.success(vo);
    }

    @ApiOperation(value = "编辑工具")
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('software:edit')")
    public AjaxResult edit(@RequestBody@Valid SoftwareDTO dto){
        LoginUser loginUser = getLoginUser();
        return softwareToolService.edit(dto, loginUser);
    }
    @ApiOperation(value = "删除工具")
    @PostMapping("/del")
    @PreAuthorize("@ss.hasPermi('software:del')")
    public AjaxResult delProject(@RequestBody String[] ids) {
        try {
            LoginUser loginUser = getLoginUser();
            List<SoftwareTool> projects = Arrays.stream(ids)
                    .map(id -> {
                        SoftwareTool st = new SoftwareTool();
                        st.setId(Integer.valueOf(id));
                        st.setDelFlag("2");
                        return st;
                    })
                    .collect(Collectors.toList());
            return softwareToolService.updateBatchById(projects) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
        } catch (Exception e) {
            logger.error("工具删除错误" + e.getMessage());
            return AjaxResult.error("删除失败");
        }
    }
}
